/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import model.File.OpenFile;

/**
 *
 * @author nusa
 */
public class PropertyMappingGUI extends javax.swing.JFrame {
    private java.util.Vector dados = new java.util.Vector();
    /**
     * Creates new form ClassMappingGUI
     */
    public PropertyMappingGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPasswordField1 = new javax.swing.JPasswordField();
        jLabelClassMapping = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaPropertyMapping = new javax.swing.JTextArea();
        jButtonSavePropertyMapping = new javax.swing.JButton();
        jButtonOpenFile = new javax.swing.JButton();

        jPasswordField1.setText("jPasswordField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabelClassMapping.setText("Mapping of Properties");

        jTextAreaPropertyMapping.setColumns(20);
        jTextAreaPropertyMapping.setRows(5);
        jScrollPane1.setViewportView(jTextAreaPropertyMapping);

        jButtonSavePropertyMapping.setText("Save");
        jButtonSavePropertyMapping.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSavePropertyMappingActionPerformed(evt);
            }
        });

        jButtonOpenFile.setText("Open File");
        jButtonOpenFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOpenFileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jScrollPane1)
                .addGap(12, 12, 12))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(219, 219, 219)
                        .addComponent(jButtonOpenFile)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSavePropertyMapping, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(239, 239, 239)
                        .addComponent(jLabelClassMapping)))
                .addContainerGap(222, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelClassMapping)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonOpenFile, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonSavePropertyMapping, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOpenFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenFileActionPerformed
        
        String line_str;
        try{
            javax.swing.JFileChooser arquivo = new javax.swing.JFileChooser();
            if(arquivo.showOpenDialog(arquivo) == javax.swing.JFileChooser.APPROVE_OPTION){
                File file = arquivo.getSelectedFile();
                String textoEntrada = "";
                java.io.FileInputStream isTwo = new
                java.io.FileInputStream( "" +file.getPath() );
                java.io.DataInputStream dsTwo = new java.io.DataInputStream(isTwo);
                dados.removeAllElements();
                while((line_str = dsTwo.readLine())!= null){
                    dados.add( line_str + " \n" );
                    textoEntrada += line_str + "\n";
                }
                jTextAreaPropertyMapping.setText(textoEntrada );

                dsTwo.close( );
            }
        }catch (java.io.IOException e) {
            javax.swing.JOptionPane.showMessageDialog( null, "File error:" + e.toString() );
        }
    }//GEN-LAST:event_jButtonOpenFileActionPerformed

    private void jButtonSavePropertyMappingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSavePropertyMappingActionPerformed

         String textSparqlQuery = jTextAreaPropertyMapping.getText();
        try{
            try (FileWriter querySparql = new FileWriter(new File("propertyMapping.txt"),false)) {
                PrintWriter out = new PrintWriter(querySparql);  
                out.printf(textSparqlQuery);
                out.close();
                querySparql.close();
            }  
        } catch (IOException ex) {
            Logger.getLogger(mainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "The Mapping of Properties it was saved!!!");
        
  
    }//GEN-LAST:event_jButtonSavePropertyMappingActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PropertyMappingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PropertyMappingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PropertyMappingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PropertyMappingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PropertyMappingGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOpenFile;
    private javax.swing.JButton jButtonSavePropertyMapping;
    private javax.swing.JLabel jLabelClassMapping;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaPropertyMapping;
    // End of variables declaration//GEN-END:variables
}
